
import { createRouter, createWebHashHistory } from 'vue-router';
import { routerArray } from './routers';
import _ from 'lodash';
import { any } from 'vue-types';

export const router = createRouter({
  history: createWebHashHistory(),
  routes: routerArray,
  strict: true,
  scrollBehavior: () => ({ left: 0, top: 0 }),
});

// ----------------------- 路由加载前 -----------------------
router.beforeEach(async (to, from, next) => {
  next();
});

// ----------------------- 路由加载后 -----------------------
router.afterEach((to:any) => {
  // 获取匹配的路由记录，倒序查找最近的 title
  const nearestWithTitle = to.matched
    .slice()
    .reverse()
    .find((r:any) => r.meta?.title)
  // 设置标题或使用默认值
  document.title = nearestWithTitle?.meta.title || '首页'
});


